# -*- coding: utf-8 -*-

def profile(func):
    def decorated(*args, **kwargs):
        import cProfile
        print '--[%s]------' % func.__name__
        cProfile.runctx('func(*args, **kwargs)', globals(), vars())
        return func(*args, **kwargs)
    return decorated


def trace_sql(func):
    def decorated(*args, **kwargs):
        from django.db import connection
        result = func(*args, **kwargs)
        t = 0
        for i, q in enumerate(connection.queries): #@UndefinedVariable
            print '%d. %s' % (i, q['sql'])
            t += float(q['time'])
        print 'total', t
        return result
    return decorated
